package com.uniriho.lxr0811_7300_bl.ui.sam

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.uniriho.lxr0811_7300_bl.base.Constant
import com.uniriho.lxr0811_7300_bl.databinding.FragmentSamBinding

class Fragment_Sam : Fragment(), View.OnClickListener {
    private lateinit var samPowerOnBtn: Button
    private lateinit var samPowerOffBtn: Button
    private lateinit var samPowerResetBtn: Button
    private lateinit var samPowerRandBtn: Button
    private lateinit var samPowerEncryBtn: Button
    private lateinit var samPowerDecryBtn: Button

    private lateinit var result: TextView

    private var samFragmentBinding: FragmentSamBinding? = null

    private  val viewmodelSam by lazy {
        ViewModel_Sam()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        samFragmentBinding = FragmentSamBinding.inflate(inflater, container, false)
        initData()
        initView()
        initEvent()
        return samFragmentBinding!!.root
    }

    private fun initData() {


    }

    private fun initEvent() {
        samPowerOnBtn.setOnClickListener(this)
        samPowerOffBtn.setOnClickListener(this)
        samPowerResetBtn.setOnClickListener(this)
        samPowerRandBtn.setOnClickListener(this)
        samPowerEncryBtn.setOnClickListener(this)
        samPowerDecryBtn.setOnClickListener(this)

    }


    private fun initView() {
        samPowerOnBtn = samFragmentBinding!!.textSamPowerOn
        samPowerOffBtn = samFragmentBinding!!.textSamPowerOff
        samPowerResetBtn = samFragmentBinding!!.textSamReset
        samPowerRandBtn = samFragmentBinding!!.textSamRand
        samPowerEncryBtn = samFragmentBinding!!.textSamENCRYPT
        samPowerDecryBtn = samFragmentBinding!!.textSamDECRYPT
        result = samFragmentBinding!!.textResult

        viewmodelSam.resultTV.observe(viewLifecycleOwner) {
            result.text = it
        }
    }


    override fun onDestroyView() {
        super.onDestroyView()
        samFragmentBinding = null
    }

    override fun onClick(p0: View?) {

        when (p0) {
            samFragmentBinding!!.textSamPowerOn -> {
                viewmodelSam.sendData(Constant.PSAM_POWERON)
            }

            samFragmentBinding!!.textSamPowerOff -> {
                viewmodelSam.sendData(Constant.PSAM_POWEROFF)
            }

            samFragmentBinding!!.textSamReset -> {
                viewmodelSam.sendData(Constant.PSAM_RESET)
            }

            samFragmentBinding!!.textSamRand -> {
                viewmodelSam.sendData(Constant.PSAM_RAND)
            }

            samFragmentBinding!!.textSamENCRYPT -> {
                viewmodelSam.sendData(Constant.PSAM_ENCRYPT)
            }

            samFragmentBinding!!.textSamDECRYPT -> {
                viewmodelSam.sendData(Constant.PSAM_DECRYPT)
            }
        }
    }
}